package leetcode_801_900;

import java.util.Stack;

public class LeeCode_856 {
    public static void main(String[] args) {
//        System.out.println(scoreOfParentheses("()"));
//        System.out.println(scoreOfParentheses("(())"));
//        System.out.println(scoreOfParentheses("()()"));
//        System.out.println(scoreOfParentheses("(()(()))"));
//        System.out.println(scoreOfParentheses("()()()"));
//        System.out.println(scoreOfParentheses("(())()"));
//        System.out.println(scoreOfParentheses("(()(()))"));
        System.out.println(scoreOfParentheses("(())()"));
        System.out.println(scoreOfParentheses("()(()())"));
    }
    private static int scoreOfParentheses(String s) {
       Stack<Integer> stack = new Stack<>();
       stack.push(0);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '('){
                stack.push(0);
            }else {
                Integer v = stack.pop();
                int top = Math.max(v * 2, 1) + stack.pop();
                stack.push(top);
            }
        }
        return stack.pop();
    }
}
